// /import core
// /commands 输入的方向
/**
 * 输入的方向
 * 
 * @function
 * @name baidu.editor.execCommand
 * @param {String}
 *            cmdName directionality执行函数的参数
 * @param {String}
 *            forward ltr从左向右输入，rtl从右向左输入
 */
(function() {

	var domUtils = baidu.editor.dom.domUtils, block = domUtils.isBlockElm, getObj = function(
			editor) {
		var startNode = editor.selection.getStart(), parents;
		if (startNode) {
			// 查找所有的是block的父亲节点
			parents = domUtils.findParents(startNode, true, block, true);
			for (var i = 0, ci; ci = parents[i++];) {
				if (ci.getAttribute('dir')) {
					return ci;
				}

			}
		}
	}, doDirectionality = function(range, editor, forward) {

		var bookmark, filterFn = function(node) {
			return node.nodeType == 1
					? !domUtils.isBookmarkNode(node)
					: !domUtils.isWhitespace(node)
		},

		obj = getObj(editor);

		if (obj && range.collapsed) {
			obj.setAttribute('dir', forward);
			return range;
		}
		bookmark = range.createBookmark();
		range.enlarge(true);
		var bookmark2 = range.createBookmark(), current = domUtils
				.getNextDomNode(bookmark2.start, false, filterFn), tmpRange = range
				.cloneRange(), tmpNode;
		while (current
				&& !(domUtils.getPosition(current, bookmark2.end) & domUtils.POSITION_FOLLOWING)) {
			if (current.nodeType == 3 || !block(current)) {
				tmpRange.setStartBefore(current);
				while (current && current !== bookmark2.end && !block(current)) {
					tmpNode = current;
					current = domUtils.getNextDomNode(current, false, null,
							function(node) {
								return !block(node)
							});
				}
				tmpRange.setEndAfter(tmpNode);
				var common = tmpRange.getCommonAncestor();
				if (!domUtils.isBody(common) && block(common)) {
					// 遍历到了block节点
					common.setAttribute('dir', forward);
					current = common;
				} else {
					// 没有遍历到，添加一个block节点
					var p = range.document.createElement('p');
					p.setAttribute('dir', forward);
					var frag = tmpRange.extractContents();
					p.appendChild(frag);
					tmpRange.insertNode(p);
					current = p;
				}

				current = domUtils.getNextDomNode(current, false, filterFn);
			} else {
				current = domUtils.getNextDomNode(current, true, filterFn);
			}
		}
		return range.moveToBookmark(bookmark2).moveToBookmark(bookmark);
	};

	baidu.editor.commands['directionality'] = {
		execCommand : function(cmdName, forward) {
			var range = new baidu.editor.dom.Range(this.document);
			if (this.currentSelectedArr && this.currentSelectedArr.length > 0) {
				for (var i = 0, ti; ti = this.currentSelectedArr[i++];) {
					if (ti.style.display != 'none')
						doDirectionality(range.selectNode(ti), this, forward);
				}
				range.selectNode(this.currentSelectedArr[0]).select()
			} else {
				range = this.selection.getRange();
				// 闭合时单独处理
				if (range.collapsed) {
					var txt = this.document.createTextNode('d');
					range.insertNode(txt);
				}
				doDirectionality(range, this, forward);
				if (txt) {
					range.setStartBefore(txt).collapse(true);
					domUtils.remove(txt);
				}

				range.select();

			}
			return true;
		},
		queryCommandValue : function() {

			var node = getObj(this);
			return node ? node.getAttribute('dir') : 'ltr'
		},
		queryCommandState : function() {
			return this.highlight ? -1 : 0;
		}
	}
})();
